/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.share;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.share.ShareConfigDao;
import com.boxin.ims.modules.wechat.entity.share.ShareConfig;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 分享转发配置Service
 * @author Jakemanse
 * @version 2013-10-01
 */
@Component
@Transactional(readOnly = true)
public class ShareConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ShareConfigService.class);
	
	@Autowired
	private ShareConfigDao shareConfigDao;
	
	public ShareConfig get(Long id) {
		return shareConfigDao.findOne(id);
	}
	
	public ShareConfig getByUserId(Long userId){
		
		DetachedCriteria dc = shareConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		List<ShareConfig> configList = shareConfigDao.find(dc);
		
		return (configList  == null || configList.isEmpty()) ? null : configList.get(0);
		
	}
	
	public Page<ShareConfig> find(Page<ShareConfig> page, ShareConfig shareConfig) {
		DetachedCriteria dc = shareConfigDao.createDetachedCriteria();
		if (shareConfig.getUser()!=null && shareConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", shareConfig.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(shareConfig.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+shareConfig.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", ShareConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return shareConfigDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ShareConfig shareConfig) {
		shareConfigDao.save(shareConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		shareConfigDao.deleteById(id);
	}
	
}
